<template>
  <el-icon style="font-size: 30px">
    <component :is="Back" @click="back"></component>
  </el-icon>
  <h2 style="background-color: #cccccc;text-align: center;margin-top: -36px">商品详情</h2>
  <el-card class="product-card" >
    <!-- 商品图片 -->
      <img :src="product?.imgSrc" alt="product" class="product-img" />
    <!-- 商品信息 -->
    <div class="product-info">
      <h3 class="product-title">{{ product?.name }}</h3>
      <p class="product-desc">{{ product?.message}}</p>
      <p class="product-price">¥{{product?.price}}</p>
      <div class="product-meta">
        <span>运费: 10</span>
        <span>剩余: 10</span>
      </div>
    </div>
    <div class="action-buttons">
      <el-button type="text">客服</el-button>
      <el-button type="text">购物车</el-button>
      <el-button type="primary" @click="addToCart">加入购物车</el-button>
      <el-button type="danger" @click="buyNow">立即购买</el-button>
    </div>
  </el-card>
</template>

<script setup lang="ts">
import {ref,onMounted} from 'vue';
import type {Product} from "@/type/product.ts";
import {ElButton, ElCard, ElMessageBox} from 'element-plus';
import {Back} from '@element-plus/icons-vue'
import {group_t1Store} from "@/store/group_t1Store.ts";
import {shopListStore} from "@/store/shopListStore.ts";
import {group_t2Store} from "@/store/group_t2Store.ts";
import {useRoute} from 'vue-router';
import router from "@/router";
const route = useRoute();
const groupStore1 = group_t1Store();
const groupStore2 = group_t2Store();
const shopStore = shopListStore();
const from=route.query.from as string | undefined;
let id=route.params.id as string;
let product=ref<Product | null>();
function getdata(){
  if (from==='group_t1'){
    product.value=groupStore1.getProductById(Number(id));
  }
  else if (from==='group_t2'){
    product.value=groupStore2.getProductById(Number(id));
  }
}

const addToCart = () => {
  console.log(product);
  var length = shopStore.getList().length;
  ElMessageBox.confirm('该商品确定加入购物车吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessageBox.alert('加入成功！', '提示', { type: 'success' });
    if(product.value){
      let appShop={id:length+1,image:product.value.imgSrc,name:product.value.name,price:product.value.price,quantity:1,checked:true}
      shopStore.setList(appShop);}
  }).catch(() => {
    ElMessageBox.alert('已取消加入', '提示', { type: 'info' });
  });

};

const buyNow = () => {
  ElMessageBox.confirm('确定购买该商品吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessageBox.alert('购买成功！', '提示', { type: 'success' });
  }).catch(() => {
    ElMessageBox.alert('已取消购买', '提示', { type: 'info' });
  });
};
function back(){
  router.back()
}
onMounted(() => {
  getdata();
})
</script>

<style scoped>
.product-card {
  width: 400px;
  margin: 1px 1px auto;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
  transition: all 0.3s;
}

.product-img {
  width: 100%;
  height: 280px;
  object-fit: cover;
  background-color: #f5f5f5;
}

.product-info {
  padding: 16px;
}

.product-title {
  font-size: 28px;
  font-weight: bold;
  margin-bottom: 8px;
  color: #333;
}

.product-desc {
  font-size: 14px;
  color: #666;
  margin-bottom: 22px;
  line-height: 2.5;
}

.product-price {
  font-size: 25px;
  color: #f56c6c;
  font-weight: bold;
  margin-bottom: 42px;
}

.product-meta {
  font-size: 12px;
  color: #999;
  display: flex;
  justify-content: space-between;
  margin-bottom: 96px;
}

.action-buttons {
  display: flex;
  justify-content: space-around;
  padding: 12px;
  border-top: 1px solid #eee;
}

.action-buttons .el-button {
  flex: 1;
  margin: 0 5px;
}
</style>